package com.kgc.javaweb.service.impl;


import com.kgc.javaweb.dao.UserDao;
import com.kgc.javaweb.dao.impl.UserDaoImpl;

import com.kgc.javaweb.dto.UserPageDTO;
import com.kgc.javaweb.entity.Notice;
import com.kgc.javaweb.entity.User;
import com.kgc.javaweb.entity.UserInfo;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.service.UserService;
import com.kgc.javaweb.util.ResponseResult;

import java.util.List;


public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    //管理员登陆注册
    @Override
    public UserInfo login(String username, String password) {

        UserInfo userInfo = userDao.findByUserNameAndPassword(username, password);
        System.out.println("userinfo = " + userInfo);
        return userInfo;

    }

    @Override
    public Boolean checkUserName(String username) {
       UserInfo userInfo=userDao.selectByName(username);
       return userInfo!=null;

    }

    @Override
    public void register(String username, String password) {

            userDao.register(username,password);
    }

    @Override
    public ResponseResult updateUserInfoPwdById(String newPassword, Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();

        try {
            userDao.updateUserInfoPwdById(newPassword,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectById(String id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            UserInfo user =  userDao.selectById(id);
            responseResult.setData(user);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult update(UserInfo userInfo) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.update(userInfo);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

//========================================================================
    //姜智博
//========================================================================
    //用户管理

    @Override
    public ResponseResult selectUserPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<User> users =  userDao.selectUserPage(userPageDTO);
            // 查询数据总条数
            Long  total =  userDao.selectUserTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(users);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectUserById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            User user =  userDao.selectUserById(id);
            responseResult.setData(user);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult updateUser(User usr) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.updateUser(usr);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult addUser(User usr) {

        ResponseResult responseResult = ResponseResult.builder().build();
        //  1. 根据用户名查询用户信息
        User user  = userDao.selectUserByUserName(usr.getUsername());
        if(user==null){
            // 2.根据查询的结果 判定用户名是否可用
            // 用户名可用   执行添加
            try {
                userDao.addUser(usr);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }
        }else{
            // 用户名不可用  直接返回结果
            responseResult.setMsg(CodeMsg.USER_EXIST.getMsg());
            responseResult.setCode(CodeMsg.USER_EXIST.getCode());
        }

        return responseResult;
    }

    @Override
    public ResponseResult deleteUser(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.deleteUser(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }


//========================================================================
// 姚俊涛
//========================================================================

    @Override
    public ResponseResult selectAdminPage1(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<User> users = userDao.selectAdminPage1(userPageDTO);
            // 查询数据总条数
            Long  total =  userDao.selectAdminTotal1(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(users);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectAdminPage2(UserPageDTO userPageDTO) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            // 分页查询   code  msg   data  count
            List<Notice> notices = userDao.selectAdminPage2(userPageDTO);
            // 查询数据总条数
            Long  total =  userDao.selectAdminTotal2(userPageDTO);

            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
            responseResult.setData(notices);
            responseResult.setCount(total);

        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public void state(Boolean checked,Integer id) {
        userDao.state(checked,id);
    }

    @Override
    public ResponseResult selectAdminById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            Notice notice = userDao.selectAdminById(id);
            responseResult.setData(notice);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult updateAdmin(Notice notice) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.updateAdmin(notice);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult addAdmin(Notice notice) {

        ResponseResult responseResult = ResponseResult.builder().build();
        //  1. 根据用户名查询用户信息
        Notice notice1  = userDao.selectAdminByUserName(notice.getTitle());
        if(notice1==null){
            // 2.根据查询的结果 判定用户名是否可用
            // 用户名可用   执行添加
            try {
                userDao.addAdmin(notice);
                responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
                responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            } catch (Exception e) {
                e.printStackTrace();
                responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
                responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            }
        }else{
            // 用户名不可用  直接返回结果
            responseResult.setMsg(CodeMsg.USER_EXIST.getMsg());
            responseResult.setCode(CodeMsg.USER_EXIST.getCode());
        }

        return responseResult;
    }

    @Override
    public ResponseResult deleteAdmin(String ids) {

        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            userDao.deleteAdmin(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult updateAdminPwdById(String newPassword, Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();

        try {
            userDao.updateAdminPwdById(newPassword,id);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }
}
